

//随着节点位置的改变动态改变箭头
Raphael.fn.drawArr = function (obj) {
	var point   = {};
	point.start = {};
	point.end   = {};
	var path1 = null;
	var bb1 = obj.obj1.getBBox(), bb2 = obj.obj2.getBBox();
	var scp = {
	    x : bb1.cx,
		y : bb1.cy
	};
	var ecp = {
		x : bb2.cx,
		y : bb2.cy
	};
	var sp={}, ep={};
	if(obj.g != null && obj.g != ""){
		var axis = buildAxis(obj.g);

		sp = connPoint(bb1, axis[0]);
		ep = connPoint(bb2, axis[axis.length-1]);
		point.start.x = sp.x;
		point.start.y = sp.y;
		point.end.x = ep.x;
		point.end.y = ep.y;
		path1 = getArrow(point.start.x, point.start.y, point.end.x, point.end.y, 8, axis);
	} else {
		sp = connPoint(bb1, ecp);
		ep = connPoint(bb2, scp);
		point.start.x = sp.x;
		point.start.y = sp.y;
		point.end.x = ep.x;
		point.end.y = ep.y;
		path1 = getArrow(point.start.x, point.start.y, point.end.x, point.end.y, 8);
	}
	//console.log('path1=='+path1);
	if (obj.arrPath) {
		obj.arrPath.attr({ path: path1 });
	} else {
		
		obj.arrPath = this.path(path1).attr({stroke: "#808080", fill: "none","stroke-width": 2});
		obj.arrPath.nodeType = "transition";
		obj.arrPath.title = obj.obj2.title;
	}
	return obj;
};

/**
 * 将坐标字符串转成数组
 */
function buildAxis(g){
	var obj = new Array();
	var xys = g.split(";");
	if(xys.length > 0) {
		for(var i=0; i<xys.length; i++){
			var xy = xys[i].split(",");
			var xyObj = {};
			xyObj.x = xy[0];
			xyObj.y = xy[1];
			obj.push(xyObj);
		}
	}
	
	return obj;
}

function connPoint(j, d) {
	var c = d, e = {
		x : j.x + j.width / 2,
		y : j.y + j.height / 2
	};
	var l = (e.y - c.y) / (e.x - c.x);
	
	l = isNaN(l) ? 0 : l;

	var k = j.height / j.width;
	var h = c.y < e.y ? -1 : 1, f = c.x < e.x ? -1 : 1, g, i;
	if (Math.abs(l) > k && h == -1) {
		g = e.y - j.height / 2;
		i = e.x + h * j.height / 2 / l
	} else {
		if (Math.abs(l) > k && h == 1) {
			g = e.y + j.height / 2;
			i = e.x + h * j.height / 2 / l
		} else {
			if (Math.abs(l) < k && f == -1) {
				g = e.y + f * j.width / 2 * l;
				i = e.x - j.width / 2
			} else {
				if (Math.abs(l) < k && f == 1) {
					g = e.y + j.width / 2 * l;
					i = e.x + j.width / 2
				}
			}
		}
	}

	return {
		x : i,
		y : g
	}
}

function getArrow(x1, y1, x2, y2, size, axis) {
	var angle = Raphael.angle(x1, y1, x2, y2);//得到两点之间的角度
	if(axis != null){
		angle = Raphael.angle(axis[axis.length-1].x, axis[axis.length-1].y, x2, y2);//得到两点之间的角度
	}
	var a45 = Raphael.rad(angle - 20);//角度转换成弧度
	var a45m = Raphael.rad(angle + 20);
	var x2a = x2 + Math.cos(a45) * size;
	var y2a = y2 + Math.sin(a45) * size;
	var x2b = x2 + Math.cos(a45m) * size;
	var y2b = y2 + Math.sin(a45m) * size;
	var result = null;
	if(axis != null){
		result = ["M", x1, y1];
		for(var i=0; i<axis.length; i++){
			result.push("L");
			result.push(axis[i].x);
			result.push(axis[i].y);
		}
		result.push("L", x2, y2, "L", x2a, y2a, "L", x2b, y2b, "L", x2, y2);

	} else {
		result = ["M", x1, y1, "L", x2, y2,  "L", x2a, y2a, "L", x2b, y2b, "L", x2, y2];
	}

	return result;
}

/**
 * 规则线
 * @param lineType
 */
function ruleLine(lineType) {
	//console.log('---------ruleLine-------------lineType---------');
	var obj = configs.curNode;
			
	var sNode = configs.curNode.obj1;
	var eNode = configs.curNode.obj2;
	var bb1 = sNode.getBBox(), bb2 = eNode.getBBox();
	var p = [
		{ x: bb1.x + bb1.width / 2, y: bb1.y - 1 },
		{ x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + 1 },
		{ x: bb1.x - 1, y: bb1.y + bb1.height / 2 },
		{ x: bb1.x + bb1.width + 1, y: bb1.y + bb1.height / 2 },
		{ x: bb2.x + bb2.width / 2, y: bb2.y - 1 },
		{ x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + 1 },
		{ x: bb2.x - 1, y: bb2.y + bb2.height / 2 },
		{ x: bb2.x + bb2.width + 1, y: bb2.y + bb2.height / 2 }
	];
	
	var _g = '';
	if(lineType == 'leftRight'){
		if((sNode.nodeX - eNode.nodeX)<0){
			var x1=p[3].x, y1=p[3].y, x2=p[6].x, y2=p[6].y;
			var len = Math.abs((x1-x2)/2);
			_g = (x1+len)+","+y1+";"+(x1+len)+","+y2;
		} else {
			var x1=p[2].x, y1=p[2].y, x2=p[7].x, y2=p[7].y;
			var len = Math.abs((x1-x2)/2);
			_g = (x1-len)+","+y1+";"+(x1-len)+","+y2;
		}
		
	} else if(lineType == 'topDown'){
		if((sNode.nodeY - eNode.nodeY)<0){
			var x1=p[1].x, y1=p[1].y, x2=p[4].x, y2=p[4].y;
			var len = Math.abs((y1-y2)/2);
			_g = x1+","+(y1+len)+";"+x2+","+(y1+len);
		} else {
			var x1=p[0].x, y1=p[0].y, x2=p[5].x, y2=p[5].y;
			var len = Math.abs((y1-y2)/2);
			_g = x1+","+(y1-len)+";"+x2+","+(y1-len);
			
		}
		
	} else if(lineType == 'borkenLine'){
		_g = p[1].x +','+p[6].y;
	} else if(lineType == 'borkenLine1'){
		_g = p[5].x +','+p[2].y;
	}

	obj.arrPath.remove();
	modifyXmlNodeAttribute(configs.curXmlNode, "g", _g);
	var _path = addVectorPath(sNode, eNode, _g);
	_path.arrPath.click(function(e){
		this.attr({fill: "#EEEEEE",'stroke':"red"});

		configs.curNode = _path;

		//连接线图标
		//configs.curXmlNode = getTtransitionXmlNode(this.nodeType, _curLine.obj1.title, _curLine.arrPath.title);  //(this.nodeType, this.title);
		configs.curXmlNode = getTtransitionXmlNode(_path.arrPath.nodeType, _path.obj1.title, _path.arrPath.title);
	    var x = (event.clientX-$("#center").scrollLeft()-35);
	    var y = (event.clientY-$("#center").scrollTop()-20);
	  //  console.log('x------'+x+'	$y-----------------------'+y);
		$('#line_operate').css("left", x).css("top", y).show();
	});
    
	$('#line_operate').hide();
}

/**
 * 获取画板鼠标真实位置
 */
function getNodeDivPosition(nodeX, nodeY, nodeType){//
	//var c_div = document.getElementById('main-panel-body');
	var offsetX=20, offsetY=15;
	if(nodeType=='fork' || nodeType=='join'){
		offsetX=7;offsetY=7;
	}
    return {
        x: nodeX - $("#center").parent().scrollLeft() + offsetX,
        y: nodeY - $("#center").parent().scrollTop() + offsetY
    };

}


/**
 * 添加矢量图节点
 * @param nodeX 节点位置X
 * @param nodeY 节点位置Y
 * @param nodeType 节点类型 start task sign fork join end subPro
 * @param nodeName 节点名称
 * @param isXml 是否同步添加XML(初始化流程图的时候不需要添加XML)
 * @returns
 */
function addVectorNode(nodeX, nodeY, nodeType, nodeName, isAudit){
	//console.log('--------------addVectorNode-----------------');
	//console.log('----------nodeX='+nodeX+'&&&nodeY='+nodeY);
	var node = configs.canvas.rect(nodeX, nodeY, configs.node.width, configs.node.height, 3);
	node.attr({'fill':"#F6F7FF", stroke:"#03689A", "stroke-width":2, 'opacity':100, 'cursor':"pointer"});

	node.nodeX = nodeX;
	node.nodeY = nodeY;
	if(isAudit == '1'){//待办
		node.attr({'fill':"#fcd653"});
		nodeMouseOverOut(node);//鼠标移入移出事件
	} else if(isAudit == '2'){//已办
		node.attr({'fill':"#FDFDB3"});
	} else {//未办
		node.attr({'stroke':"#03689A"});
	}
	var nodeHtml = '';	

	switch(nodeType){
		case "start":
			node.id = 'start';
			node.title = nodeName||"流程开始";
			node.nodeType = "start";
			nodeHtml = "<div id='start' class='node-start node'><b>"+node.title+"</b></div>";
			break;
		case "task":
			node.id = 'task'+configs.node.taskNum;
			node.title = nodeName||"任务节点" + configs.node.taskNum;
			node.nodeType = "task";
			nodeHtml = "<div id='task"+configs.node.taskNum+"' class='node-task node'><b>"+node.title+"</b></div>";
			configs.node.taskNum ++;
			break;
		case "sign":
			node.id = 'sign'+configs.node.signNum;
			node.title = nodeName||"会签任务" + configs.node.signNum;
			node.nodeType = "sign";
			nodeHtml = "<div id='sign"+configs.node.signNum+"' class='node-sign node'><b>"+node.title+"</b></div>";
			configs.node.signNum ++;
			break;
		case "end":
			node.id = 'end'+configs.node.endNum;
			node.title = nodeName||"流程结束"+configs.node.endNum;
			node.nodeType = "end";
			nodeHtml = "<div id='end"+configs.node.endNum+"' class='node-end node'><b>"+node.title+"</b></div>";
			configs.node.endNum ++;
			break;
		case "fork":
			node.id = "fork" + configs.node.forkNum;
			node.title = nodeName||"分支" + configs.node.forkNum;
			node.nodeType = "fork";
			node.attr('width', 50);
			nodeHtml = "<div id='fork"+configs.node.forkNum+"' class='node node-fork node-half'>&nbsp;</div>";
			configs.node.forkNum ++;
			break;
		case "join":
			node.id = "join" + configs.node.joinNum;
			node.title = nodeName||"合并" + configs.node.joinNum;
			node.nodeType = "join";
			node.attr('width', 50);
			nodeHtml = "<div id='join"+configs.node.joinNum+"' class='node node-join node-half'>&nbsp;</div>";
			configs.node.joinNum ++;
			break;
		case "subPro":
			node.id = "subPro" + configs.node.subProNum;
			node.title = nodeName||"子流程" + configs.node.subProNum;
			node.nodeType = "subPro";
			nodeHtml = "<div id='subPro"+configs.node.subProNum+"' class='node_sub_process node'><b>"+node.title+"</b></div>";
			configs.node.subProNum ++;
			break;
		case "decision":
			node.id = "decision" + configs.node.boolNum;
			node.title = nodeName||"决策" + configs.node.boolNum;
			node.nodeType = "decision";
			nodeHtml = "<div id='decision"+configs.node.boolNum+"' class='node-bool node'><b>"+node.title+"</b></div>";
			configs.node.boolNum ++;
			break;
		default:
	}

	//定位节点上的文字
	$('#center').append(nodeHtml);
	var divP = getNodeDivPosition(nodeX, nodeY, nodeType);
	//console.log("nodeId======="+node.id);
	//console.log($('#'+node.id));
	//console.log(divP);
	$('#'+node.id).css("left", divP.x).css("top", divP.y);
	/**
	//添加XML节点
	if(isXml) { 
		_node.drag(dragMove, dragStart, dragUp);
		_node.click(function(e){
			this.attr({'fill': "#F6F7FF",'stroke':"red"});
			configs.curNode = this;
			configs.curXmlNode = getXmlNode(this.nodeType, this.title);
			//当前XML节点
			//showNodeProperty();
		});	
		addXmlNode(_node);//添加XML节点
	
	}*/
	
	return node;
}

/**
 * 添加流程链接线
 */
function addVectorPath(sNode, eNode, _g){
	//console.log('-----------addVectorPath--------------------------------');
	//console.log(sNode);
	//console.log('sNode.title=='+sNode.title+'&&sNode.nodeX=='+sNode.nodeX+'	&&sNode.nodeY==='+sNode.nodeY);
	//console.log('eNode.title=='+eNode.title+'&&eNode.nodeX=='+eNode.nodeX+'	&&eNode.nodeY==='+eNode.nodeY);
	var _path = configs.canvas.drawArr({ obj1: sNode, obj2: eNode , g:_g });
	configs.connections.push(_path);
	/* 
	_path.arrPath.click(function(){
		this.attr({fill: "#EEEEEE",'stroke':"red"});
		configs.curNode = _path;
		//连接线图标
		configs.curXmlNode = getTtransitionXmlNode(_path.arrPath.nodeType, sNode.title, _path.arrPath.title);  //(this.nodeType, this.title);
		var c_div = document.getElementById('main-panel-body');
	    var x = (event.clientX-c_div.scrollLeft-35);
	    var y = (event.clientY-c_div.scrollTop-20);
	    
		Ext.fly('line_operate').show().setXY([x, y]);
		
	});*/
	return _path;
}


